import json

import logging
import requests
from django.shortcuts import render

# Create your views here.
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from searchApi.config import webSiteInfo
from webSite.models import item
# from demo.models import User
# from demo.serializer import UserSerializer
service=webSiteInfo()
@api_view(['GET'])
def getScrapyUrls(request):
    data = []
    datas = service.getAllItems()
    for val in datas:
        data.extend(val.url.split(','))
    obj = {'code': 0, 'data': data, 'total': len(data)}
    return Response(obj, status=status.HTTP_200_OK)
# 获取网页的子网页的标记@url
@api_view(['GET'])
def getPageFlagByUrl(request):
    url = request.GET.get('url')
    data={'pageFlag':None}
    response = {'code': -1, 'data': data}
    for obj in webSiteInfo.scrapyObj:
        if url==obj['originUrl']:
            data['pageFlag']=obj['pageFlag']
            response['code']=0
            break
    return Response(response, status=status.HTTP_200_OK)
# 获取对应的网页信息@originUrl
@api_view(['GET'])
def getItemInfoByOriginUrl(request):
    curOriginUrl=request.GET.get('originUrl')
    response={'code':-1,
              'data':{'pageFlag':None,'type':None,
                      'group':None ,'originName':None,
                      'originName':None,'path':None}
              }
    items=service.getItemInfoByOriginUrl(curOriginUrl)
    for item in items:
        response['data']['originUrl']=item.originUrl
        response['data']['originName']=item.originName
        response['data']['pageFlag']=item.pageFlag
        response['data']['path']=item.path
        response['data']['url']=item.url.split(',')
        response['data']['type']=item.type
        response['data']['group']=item.group
        response['code']=0
    return Response(response,status=status.HTTP_200_OK)

# 从数据库获取所有爬取的信息
@api_view(['GET'])
def getWebSitesdb(request):
    datas=service.getAllItems()
    data=[]
    for val in datas:
        obj={}
        obj['pageFlag']=val.pageFlag
        obj['url']=val.url.split(',')
        obj['originName']=val.originName
        obj['originUrl']=val.originUrl
        obj['type']=val.type
        obj['group']=val.group
        obj['path']=val.path
        data.append(obj)
    obj={'code':0,'data':data,'total':len(data)}
    return Response(obj,status=status.HTTP_200_OK)

@api_view(['GET'])
def getPageFlagByOriginUrl(request):
    originUrl = request.GET.get('originUrl')
    data = {'pageFlag': None}
    items=service.getflagByUrl(originUrl)
    response = {'code': -1, 'data': data}
    for item in items:
        response['data']['pageFlag'] = item.pageFlag
        response['code'] = 0
    return Response(response, status=status.HTTP_200_OK)


